 /* =========================================================
  * videoHelper.js v1.0.0   创建人：卢波  2015-08-29
  * 调用方法：
  * html：<div id="vieodiv"></div>
  *  videoHelper.init(33,"videoDiv","Video_Ocx");// 初始化配置信息 videoDiv:视频显示在某个节点的ID
  *  videoHelper.startRealplay(); //播放实时视频
  * =========================================================
  *修改记录：
  *
  *
  *
  *
  *
  * ========================================================= */

 var videoHelper = {
     stationId: null,
     cameraId: null,
     videoObject: {
         0: { //海康
             clsid: "D5E14042-7BF6-4E24-8B01-2F453E8154D7",
             AddIn: "HK"
         },
         1: { //大华
             clsid: "D3E383B6-765D-448D-9476-DFD8B499926D",
             AddIn: "DH"
         },
         2: { //力维
             clsid: "369576C1-8D06-4734-BC4D-153998E087F4",
             AddIn: "LW"
         }
     },
     /*视频配置信息-默认值为返回的数组第一个对象
        videoCfg.ServerIp//视频服务器IP
        videoCfg.Port  //视频服务器端口
        videoCfg.UserName //登录用户名
        videoCfg.UserPwd  //登录密码
        videoCfg.EncoderId 
        videoCfg.CameraPort
        videoCfg.CameraId //视频通道号
        videoCfg.TypeId  //视频类型 0：海康  1：大华  2：力维
     */
     videoCfg: null,
     videoCfgRows: null, //视频配置信息数组
     objectId: null,
     videoDiv: null,
     init: function(stationId, cameraId, videoDiv, objectId) {
         if (stationId !== this.stationId || cameraId !== this.cameraId) {
             this.getVideoConfig(stationId, cameraId);
             this.stationId = stationId;
			 this.cameraId = cameraId;
         }
         this.objectId = objectId;
         if (videoDiv) {
             this.videoDiv = videoDiv;
         } else {
             this.videoDiv = "vieodiv";
         }
         if (objectId) {
             this.objectId = objectId;
         } else {
             this.objectId = "Video_Ocx";
         }
     },
     innerVideoObject: function(typeId) {
         var videoDiv = $("#" + this.videoDiv);
         videoDiv.html("<object id='" + this.objectId + "'  style='height:100%; width:99%;' codebase='/" + this.videoObject[typeId].AddIn + ".cab' classid='clsid:" + this.videoObject[typeId].clsid + "'><param name=\"PreviewBottomToolBar\" value=0><param name=\"PreviewCloseBnt\" value=1><param name=\"wmode\" value=\"transparent\"></object>");
         //播放插件未安装，弹出下载界面
		 
		 var tmpCtrl=eval('document.all.'+this.objectId);
		 var tmpObj=eval('document.all.'+this.objectId+".object");
         if (tmpCtrl== null || tmpObj === null) {
             videoDiv.html("<p>温馨提示：如果控件不能在线注册，请点击<a target=\"_blank\" style='color:#337AB7;' href=\"/" + this.videoObject[typeId].AddIn + ".exe\">下载注册文件</a>进行手动注册，执行后请刷新页面。</p>");
             return;
         }
     },
     playHK: function(videoPlugin) {
         videoPlugin.SetWndNum(1);
         videoPlugin.PtzSpeed(2);
         videoPlugin.SelWindow(0);
         videoPlugin.StartTask_Preview_InWnd(HKVideoXml[this.stationId], 0);
     },
     playDF: function(videoPlugin) {
         //登录视频平台
         var iResult = videoPlugin.DPSDK_Login(this.videoCfg.ServerIp, this.videoCfg.Port, this.videoCfg.UserName, this.videoCfg.UserPwd);
         //videoPlugin.DPSDK_LoadDGroupInfo();
         var gWndId = videoPlugin.DPSDK_CreateSmartWnd(0, 0, 100, 100);
         var nWndCount = 1;
         videoPlugin.DPSDK_SetWndCount(gWndId, nWndCount);
         videoPlugin.DPSDK_SetSelWnd(gWndId, 0);
         var nWndNo = videoPlugin.DPSDK_GetSelWnd(gWndId);
         videoPlugin.DPSDK_StartRealplayByWndNo(gWndId, nWndNo, this.videoCfg.CameraId, "1", "1", "1");
     },
     playLW: function(videoPlugin) {
         //初始化视频参数
         var Error = {
             "-536861928": "客户端已经有用户登录",
             "-536862186": "服务器未连接",
             "-536862192": "连接服务器失败",
             "-536862191": "请求超时",
             "-536862461": "请求视频路由失败",
             "-536862459": "无可用的视频路由",
             "-536862446": "设备未注册",
             "-536862190": "连接服务器中断"
         }
         var ret = videoPlugin.LogIn(this.videoCfg.ServerIp, this.videoCfg.Port, this.videoCfg.UserName, this.videoCfg.UserPwd);
         if (parseInt(ret) !== -536861928) {
             if (parseInt(ret) < 0) {
                 if (Error[ret]) {
                    if($("#btndataInput").length>0)
                    {$("#btndataInput").attr("disabled","disabled");}
                     alert("用户登录失败：" + Error[ret]);
                 } else {
                     if($("#btndataInput").length>0)
                    {$("#btndataInput").attr("disabled","disabled");}
                     alert("用户登录失败：" + ret);
                 }
                 return;
             }
         }
         videoPlugin.StateBarDisplay(1);
         videoPlugin.PlayVideo(this.videoCfg.CameraId, this.videoCfg.ServerIp, this.videoCfg.Port);
     },
     startRealplay: function() {
         if (this.videoCfg === null) {
             $("#" + this.videoDiv).html("<img style='width:100%; height:100%;' src=\"images/novideo.gif\" />");
             return;
         }
         var TypeId = parseInt(this.videoCfg.TypeId);
         this.innerVideoObject(TypeId);
         var videoPlugin = document.getElementById(this.objectId);
         if (videoPlugin === null) {
             return;
         }
         switch (TypeId) {
             case 0: //播放海康视频
                 this.playHK(videoPlugin);
                 break;
             case 1: //播放大华视频
                 this.playDF(videoPlugin);
                 break;
             case 2: //播放力维视频
                 this.playLW(videoPlugin);
                 break;
             default:
            	 break;
         }
     },
     historyInit: function() {
         if (this.videoCfg === null) {
             $("#" + this.videoDiv).html("<img src=\"images/novideo.gif\" />");
             return;
         }
         var TypeId = parseInt(this.videoCfg.TypeId);
         this.innerVideoObject(TypeId);
     },
     getVideoConfig: function(stationId, cameraId) {
         var url = "../MCSHandle.ashx?key=SiteVideoList";
         $.ajax({
             url: url,
             async: false, // 注意此处需要同步，因为返回完数据后，下面才能让结果的第一条selected  
             type: "POST",
             dataType: "json",
             data: {
                 StationId: stationId,
				 CameraId:cameraId,
                 resultType: 2
             },
             success: function(resp) {
                 videoHelper.videoCfg = null;
                 videoHelper.videoCfgRows = null;
                 if (resp.rows !== null && resp.rows.length > 0) {
                     videoHelper.videoCfg = resp.rows[0];
                     videoHelper.videoCfgRows = resp.rows;
                 }
             }
         });
         /*var videoCfg = {};
         videoCfg.ServerIp = "10.45.157.252";
         videoCfg.Port = "8000";
         videoCfg.UserName = "znv";
         videoCfg.UserPwd = "888888";
         videoCfg.EncoderId = "010101111000101";
         videoCfg.CameraPort = "1";
         videoCfg.CameraId = "010101121000481";
         videoCfg.TypeId = "2";
         videoHelper.videoCfg = videoCfg;
         videoHelper.videoCfgRows = [videoCfg];*/
     }
 }
